<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* * *********************************
 * UniPortal - Login module
* @Author: Le Thuy Trieu
* @date: 2011/12/28
* ******************************** */
class Login extends MY_Controller {

	function __construct() {
		parent::__construct();

		$this->template->set_template('login');
		parent::add_jQueryMobile_css();
		parent::add_jQueryMobile_js();
		$this->load->model("user_accounts_model", "user_model");
	}

	public function index($email = '', $password = '', $error_msg = '')
	{
		if($this->_is_connect()){
			redirect(site_url('users'), 'refresh');
			return TRUE;
		}else{
			$data['email'] = $email;
			$data['password'] = $password;
			$data['login_url'] = site_url('login/authen');
			$data['errormessage'] = $error_msg;
			$this->template->parse_view("content", "view_signin", $data);
			$this->template->render();
				
		}
	}

	public function authen(){
		if($this->_is_connect()){
			redirect(site_url('users'));
			return TRUE;
		}
		$email = $this->input->post("email");
		$password = $this->input->post("password");
		if(!empty($email)){
			$user = $this->user_model->_connect($email, $password);

			if(!empty($user)){
				$this->session->set_userdata('email', $user['email']);
				$this->load->model('global_setting_model', 'setting_model');
				$this->user_model->_save_session($user);
				$url = $this->setting_model->_get('user_default_page', USER_DEFAULT_PAGE);
				redirect($url);
			} else {
				$this->index($email, $password, $this->lang->line('err_msg'));
			}
		}else{
			$this->index($email, $password, $this->lang->line('empty_err_msg'));
		}
	}


}
